package com.gamenew.bluetoothdemo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gamenew.bluetoothdemo.R;
import com.gamenew.bluetoothdemo.bean.BluetoothBean;

import java.util.List;

/**
 * Created by Administrator on 2017/9/14.
 */

public class BluetoothListAdapter extends BaseAdapter {

    private List<BluetoothBean> list;
    private Context context;

    public BluetoothListAdapter(Context context, List<BluetoothBean> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.adapter_scan_result, null);
            holder = new ViewHolder();
            convertView.setTag(holder);
            holder.txt_name = convertView.findViewById(R.id.txt_name);
            holder.txt_mac = convertView.findViewById(R.id.txt_mac);
            holder.txt_rssi = convertView.findViewById(R.id.txt_rssi);
        }

        BluetoothBean result = list.get(i);
        String name = result.getName();
        String mac = result.getMac();
        int rssi = result.getRssi();

        holder.txt_name.setText(name);
        holder.txt_mac.setText(mac);
        holder.txt_rssi.setText(String.valueOf(rssi));
        return convertView;
    }

    class ViewHolder {
        TextView txt_name;
        TextView txt_mac;
        TextView txt_rssi;
    }
}
